﻿using ClientModels;
using Microsoft.Win32;
using RRQMMVVM;
using System;
using System.IO;
using System.Windows;
using ViewModel.Models;

namespace ViewModel.ViewModels
{
    public class RecentlyContactItemViewModel : ContactItemViewModel
    {
        public RecentlyContactItemViewModel()
        {
            this.DeleteItemCommand = new ExecuteCommand(DeleteItem);
            this.SendMessageCommand = new ExecuteCommand(SendMessage);
            this.SendFileCommand = new ExecuteCommand(SendFile);
            this.ChatMesItemsSource = new RRQMList<ChatMessageItem>();

            this.NewFriendRequestItems = new RRQMList<NewFriendRequestItemViewModel>();
        }

        #region Command

        public ExecuteCommand DeleteItemCommand { get; set; }
        public ExecuteCommand SendMessageCommand { get; set; }
        public ExecuteCommand SendFileCommand { get; set; }

        #endregion Command

        #region 属性

        public RRQMList<RecentlyContactItemViewModel> Parent { get; set; }

        /// <summary>
        /// 联系人条目类型
        /// </summary>
        public ContactItemType ContactItemType { get; set; }

        private string messageText = string.Empty;

        /// <summary>
        /// 自己输入的文字
        /// </summary>
        public string MessageText
        {
            get { return messageText; }
            set
            {
                messageText = value;
                if (messageText.Length > 0)
                {
                    this.SendButtonEnable = true;
                }
                else
                {
                    this.SendButtonEnable = false;
                }
                OnPropertyChanged();
            }
        }

        private RRQMList<ChatMessageItem> chatMesItemsSource;

        public RRQMList<ChatMessageItem> ChatMesItemsSource
        {
            get { return chatMesItemsSource; }
            set { chatMesItemsSource = value; OnPropertyChanged(); }
        }

        private RRQMList<NewFriendRequestItemViewModel> newFriendRequestItems;

        public RRQMList<NewFriendRequestItemViewModel> NewFriendRequestItems
        {
            get { return newFriendRequestItems; }
            set { newFriendRequestItems = value; OnPropertyChanged(); }
        }

        private DateTime lastSessionTime;

        public DateTime LastSessionTime
        {
            get { return lastSessionTime; }
            set { lastSessionTime = value; OnPropertyChanged(); }
        }

        private string lastSessionText;

        public string LastSessionText
        {
            get { return lastSessionText; }
            set { lastSessionText = value; OnPropertyChanged(); }
        }

        private int unreadNumber;

        public int UnreadNumber
        {
            get { return unreadNumber; }
            set
            {
                unreadNumber = value;
                if (value == 0)
                {
                    EllipseVisible = Visibility.Hidden;
                }
                else
                {
                    EllipseVisible = Visibility.Visible;
                }
                OnPropertyChanged();
            }
        }

        private Visibility ellipseVisible = Visibility.Hidden;

        public Visibility EllipseVisible
        {
            get { return ellipseVisible; }
            set { ellipseVisible = value; OnPropertyChanged(); }
        }

        private bool sendButtonEnable = false;

        public bool SendButtonEnable
        {
            get { return sendButtonEnable; }
            set { sendButtonEnable = value; OnPropertyChanged(); }
        }

        #endregion 属性

        public void test()
        {
            for (int i = 0; i < 20; i++)
            {
                ChatMessageItem messageItem = new ChatMessageItem();
                messageItem.MessageOwner = i % 2 == 0 ? MessageOwner.Friend : MessageOwner.Self;

                messageItem.Message = "张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三" + "“" + NickName + "”";
                this.ChatMesItemsSource.Add(messageItem);
            }
        }

        private void DeleteItem()
        {
            this.Parent.Remove(this);
            MainViewModel.Instance.WindowBody.UnReadMesNum -= this.UnreadNumber;
            MainViewModel.Instance.WindowBody.RecentlyContactText = string.Empty;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        private void SendMessage()
        {
            //if (TransferClient.SendMessage(this.Account, this.MessageText))
            //{
            //    ChatMessageItem messageItem = new ChatMessageItem();
            //    messageItem.Message = this.messageText;
            //    messageItem.MessageOwner = MessageOwner.Self;
            //    messageItem.MessageType = MessageType.文本消息;
            //    this.MessageText = string.Empty;

            //    this.ChatMesItemsSource.Add(messageItem);
            //}
        }

        /// <summary>
        /// 发送文件
        /// </summary>
        private void SendFile()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.ShowDialog();
            if (openFile.FileName != null && openFile.FileName != string.Empty)
            {
                ChatMessageItem messageItem = new ChatMessageItem();
                messageItem.Message = this.messageText;

                FileInfo fileInfo = new FileInfo(openFile.FileName);
                messageItem.UploadFile = new UploadFileViewModel();
                messageItem.UploadFile.FilePath = fileInfo.FullName;
                messageItem.UploadFile.FileName = fileInfo.Name;
                messageItem.UploadFile.FileLength = string.Format("{0}Mb", ((float)fileInfo.Length / 1048576).ToString("0.00"));
                messageItem.UploadFile.Message = "准备发送";
                messageItem.UploadFile.TransferSpeed = "0kb/s";
                messageItem.UploadFile.UploadProgressValue = 0;
                messageItem.MessageOwner = MessageOwner.Self;
                messageItem.MessageType = MessageType.文件信息;

                messageItem.UploadFile.StartUpload(this.Account);
                this.ChatMesItemsSource.Add(messageItem);
            }
        }
    }
}